package interpreter.bytecodes.debuggerByteCodes;

import interpreter.VirtualMachine;
import interpreter.bytecodes.ByteCode;
import interpreter.debugger.DebugVirtualMachine;
import java.util.Vector;

/**
 *
 * @author Kris Selbekk
 */
public class FormalByteCode extends ByteCode {

    private String id;
    private int value;

    public void exec( VirtualMachine rt ) {
        DebugVirtualMachine vm = (DebugVirtualMachine) rt;

        int val = vm.getStackAt(vm.getStackSize() - (value + 1) );

        // Adds the formal
        vm.enterSymbol(id, val);

        // Check if isStepIn should be false
        if( vm.formalCount() == vm.argCount() )
            vm.isStepIn(false);

    }
    public void init( Vector<String> args ) {
        try {
            value = Integer.parseInt(args.get(1));

            if( args.size() >= 2)
                    id = args.get(0);
        } catch( NumberFormatException nfe ) {
            // If bad format
            System.out.println("Bad format!");
            System.exit(-1);
        } catch( NullPointerException npe ) {
            // If argument missing
            System.out.println("Argument given = null.");
            System.exit(-1);
        }
    }

    public Object getArgs() {
        return id + " " + value;
    }
}
